上一篇有教怎麼創建對話框,這篇就不再介紹,還有不懂怎麼創建對話框,可以去看上一篇。
這篇主要介紹,Dialog 常用4種對話框,以下是比較常會用到的4種對話框:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("購物車");
dialog.setMessage("商品");
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setPositiveButton("確認購買", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.show();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("購物車");
dialog.setMessage("商品");
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setPositiveButton("確認購買", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.setNeutralButton("+ or -", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.show();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {"葡萄", "橘子", "蘋果", "酪梨"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("商品列表");
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你點擊了" + items[i], Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("進度");
dialog.setMessage("等待中...");
dialog.show();
}
}